//
//  FIDPickerView.m
//  FIDPickerView
//
//  Created by Fidetro on 16/9/12.
//  Copyright © 2016年 Fidetro. All rights reserved.
//

#import "FIDPickerView.h"

#define kLabelFontSize (30)

@interface FIDPickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>

/** <#Description#> **/
@property(nonatomic,assign) NSInteger selectedIndex;

@end

@implementation FIDPickerView



- (instancetype)initWithDataSource:(NSArray *)dataArray detailSource:(NSArray *)detailArray{
    self = [super init];
    if (self) {
        
        self.dataArray = dataArray;
        self.detailArray = detailArray;
        self.delegate = self;
        self.dataSource = self;
        [self masLayoutSubviews];
    }
    return self;
}

- (void)masLayoutSubviews{
    __weak UIView *superView = self;
    UIView *selectedView = [[UIView alloc] init];
    [self addSubview:selectedView];
    selectedView.backgroundColor = [UIColor redColor];
    [selectedView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(superView.mas_centerY);
        make.left.right.mas_equalTo(superView.mas_left);
        make.height.mas_equalTo(px1920Height(100));
    }];
//    [self.hourLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.mas_equalTo(superView.mas_centerY).with.offset(px1080Width(10));
//        if (SCREEN_WIDTH == 414) {
//            make.right.equalTo(superView.mas_centerX).with.offset(-px1080Width(125));
//        } else if (SCREEN_WIDTH == 375) {
//            make.right.equalTo(superView.mas_centerX).with.offset(-px1080Width(140));
//        } else if (SCREEN_WIDTH == 320) {
//            make.right.equalTo(superView.mas_centerX).with.offset(-px1080Width(170));
//        }
//        make.height.width.mas_equalTo(px1080Width(50));
//    }];
//    NSLog(@"width=%f",SCREEN_WIDTH);
//    
//    [self.minuteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.mas_equalTo(superView.mas_centerY).with.offset(px1080Width(10));
//        if (SCREEN_WIDTH == 414) {
//            make.right.equalTo(superView.mas_centerX).with.offset(px1080Width(90));
//        } else if (SCREEN_WIDTH == 375) {
//            make.right.equalTo(superView.mas_centerX).with.offset(px1080Width(100));
//        } else if (SCREEN_WIDTH == 320) {
//            make.right.equalTo(superView.mas_centerX).with.offset(px1080Width(110));
//        }
//        make.height.width.mas_equalTo(px1080Width(50));
//    }];
//    
//    [self.secondLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.mas_equalTo(superView.mas_centerY).with.offset(px1080Width(10));
//        if (SCREEN_WIDTH == 414) {
//            make.left.equalTo(superView.mas_centerX).with.offset(px1080Width(245));
//        } else if (SCREEN_WIDTH == 375) {
//            make.left.equalTo(superView.mas_centerX).with.offset(px1080Width(275));
//        } else if (SCREEN_WIDTH == 320) {
//            make.left.equalTo(superView.mas_centerX).with.offset(px1080Width(325));
//        }
////        make.left.equalTo(weakSelf.minuteLabel.mas_right).with.offset(px1080Width(150));
//        make.height.width.mas_equalTo(px1080Width(50));
//    }];
}

#pragma mark - --------------------------UIPickerViewDataSource--------------------------
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    
    return self.dataArray.count;
    
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    return ((NSArray *)((self.dataArray[component]))).count;
    
}
#pragma mark - --------------------------UIPickerViewDelegate--------------------
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    return self.dataArray[component][row];
    
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
    UILabel *rowLabel = [[UILabel alloc]init];
    rowLabel.textAlignment = NSTextAlignmentCenter;
    
    // 选中时放大
//    if ((self.hourRow == row && component == 0) || (self.minuteRow == row && component == 1) || (self.secondRow == row && component == 2)) {
//        [rowLabel setFont:[UIFont systemFontOfSize:kLabelFontSize]];
//    } else{
//        [rowLabel setFont:[UIFont systemFontOfSize:20]];
//    }
//
//    if (self.detailArray.count != self.dataArray.count) {
//        
//        rowLabel.text = [NSString stringWithFormat:@"%@",self.dataArray[component][row]];
//    }else
//        
//        rowLabel.text = [NSString stringWithFormat:@"%@%@",self.dataArray[component][row],self.detailArray[component]];
    
    // 隐藏分割线：因为添加了三个固定Label，因此线的位置在4和5
//    ((UIView *)[pickerView.subviews objectAtIndex:4]).backgroundColor = [UIColor clearColor];
//    ((UIView *)[pickerView.subviews objectAtIndex:5]).backgroundColor = [UIColor clearColor];
    return rowLabel;
    
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    
    if (self.detailArray.count != self.dataArray.count) {
        
        return [[NSString stringWithFormat:@"%@",[self.dataArray[component] lastObject]] sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kLabelFontSize]}].width+40;//防止选中的时候会偏移
    }else
        
        return [[NSString stringWithFormat:@"%@",[self.dataArray[component] lastObject]] sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kLabelFontSize]}].width+[[NSString stringWithFormat:@"%@",self.detailArray[component]] sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kLabelFontSize]}].width+40;//防止选中的时候会偏移
    
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return px1920Height(100);
}


//选择pickerView显示内容的时候，需要重写这个方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
//    switch (component) {
//        case 0:
//            self.hourRow = row;
//            break;
//        case 1:
//            self.minuteRow = row;
//            break;
//        case 2:
//            self.secondRow = row;
//            break;
//            
//        default:
//            break;
//    }
    [self reloadAllComponents];
//    NSLog(@"滚动选择的内容 %@",self.dataArray[component][row]);
    
}



- (UILabel *)placeLabel
{
    if(!_placeLabel)
    {
        UIView *superView = self;
        _placeLabel = [[UILabel alloc]init];
        [superView addSubview:_placeLabel];
        [_placeLabel setFont:[UIFont systemFontOfSize:15]];
        [_placeLabel setTextColor:[UIColor whiteColor]];
    }
    return _placeLabel;
}



@end
